using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using System;
using System.IO;
using System.Xml.Linq;
namespace MicroMsg.Scene
{
	public class EmojiImageService
	{
		private const string TAG = "EmojiImageService";
		private const string XMLMOD = "<msg><emoji fromusername = \"\" tousername = \"\" type=\"1\" idbuffer=\"\" md5=\"\" len = \"0\" androidmd5=\"\" androidlen=\"0\"></emoji> <gameext type=\"0\" content=\"0\" ></gameext></msg>";
		public static EmojiDownloadInfo getEmojiDownloadInfo(string strXml)
		{
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			if (!strXml.StartsWith("<"))
			{
				Log.smethod_5("EmojiImageService", "strXml need cut");
				int num = strXml.IndexOf("<", 0);
				if (num < 0)
				{
					return null;
				}
				strXml = strXml.Substring(num);
			}
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			EmojiDownloadInfo emojiDownloadInfo = new EmojiDownloadInfo();
			EmojiDownloadInfo result;
			try
			{
				XDocument xDocument = XDocument.Parse(strXml);
				XElement xElement = xDocument.Element("msg");
				if (xElement != null)
				{
					XElement xElement2 = xElement.Element("emoji");
					if (xElement2 != null)
					{
						emojiDownloadInfo.strID = xElement2.Attribute("idbuffer").get_Value();
						emojiDownloadInfo.strMd5 = xElement2.Attribute("androidmd5").get_Value();
						emojiDownloadInfo.nType = int.Parse(xElement2.Attribute("type").get_Value());
						emojiDownloadInfo.nTotalLen = int.Parse(xElement2.Attribute("androidlen").get_Value());
					}
					XElement xElement3 = xElement.Element("gameext");
					if (xElement3 != null)
					{
						emojiDownloadInfo.nGameExtType = int.Parse(xElement3.Attribute("type").get_Value());
						emojiDownloadInfo.strGameExtContent = xElement3.Attribute("content").get_Value();
					}
					result = emojiDownloadInfo;
				}
				else
				{
					result = null;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("EmojiImageService", ex.get_StackTrace());
				result = null;
			}
			return result;
		}
		public static EmojiInfo getEmojiInfo(string strXml)
		{
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			if (!strXml.StartsWith("<"))
			{
				Log.smethod_5("EmojiImageService", "strXml need cut");
				int num = strXml.IndexOf("<", 0);
				if (num < 0)
				{
					return null;
				}
				strXml = strXml.Substring(num);
			}
			if (string.IsNullOrEmpty(strXml))
			{
				return null;
			}
			EmojiInfo emojiInfo = new EmojiInfo();
			EmojiInfo result;
			try
			{
				XDocument xDocument = XDocument.Parse(strXml);
				XElement xElement = xDocument.Element("msg");
				if (xElement != null)
				{
					XElement xElement2 = xElement.Element("emoji");
					if (xElement2 != null)
					{
						emojiInfo.toUserName = xElement2.Attribute("tousername").get_Value();
						emojiInfo.strMd5 = xElement2.Attribute("androidmd5").get_Value();
						emojiInfo.nImgType = int.Parse(xElement2.Attribute("type").get_Value());
						emojiInfo.nTotalLen = int.Parse(xElement2.Attribute("androidlen").get_Value());
					}
					XElement xElement3 = xElement.Element("gameext");
					if (xElement3 != null)
					{
						emojiInfo.gameExtInfo = new GameExtInfo();
						emojiInfo.gameExtInfo.nGameType = int.Parse(xElement3.Attribute("type").get_Value());
						emojiInfo.gameExtInfo.strContent = xElement3.Attribute("content").get_Value();
					}
					result = emojiInfo;
				}
				else
				{
					result = null;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("EmojiImageService", ex.get_StackTrace());
				result = null;
			}
			return result;
		}
		public static string getEmojiInfoXml(EmojiInfo info)
		{
			if (AccountMgr.getCurAccount() == null)
			{
				Log.smethod_1("EmojiImageService", "AccountMgr.getCurAccount() == null");
				return null;
			}
			if (info == null)
			{
				return null;
			}
			string result;
			try
			{
				XDocument xDocument = XDocument.Parse("<msg><emoji fromusername = \"\" tousername = \"\" type=\"1\" idbuffer=\"\" md5=\"\" len = \"0\" androidmd5=\"\" androidlen=\"0\"></emoji> <gameext type=\"0\" content=\"0\" ></gameext></msg>");
				XElement xElement = xDocument.Element("msg");
				if (xElement != null)
				{
					XElement xElement2 = xElement.Element("emoji");
					if (xElement2 != null)
					{
						xElement2.Attribute("fromusername").set_Value(AccountMgr.getCurAccount().strUsrName);
						xElement2.Attribute("tousername").set_Value(info.toUserName);
						xElement2.Attribute("type").set_Value(string.Concat(info.nImgType));
						xElement2.Attribute("androidmd5").set_Value(info.strMd5);
						xElement2.Attribute("androidlen").set_Value(string.Concat(info.nTotalLen));
					}
					XElement xElement3 = xElement.Element("gameext");
					if (xElement3 != null)
					{
						xElement3.Attribute("type").set_Value(string.Concat(info.gameExtInfo.nGameType));
						if (info.gameExtInfo.strContent == null)
						{
							xElement3.Attribute("content").set_Value("0");
						}
						else
						{
							xElement3.Attribute("content").set_Value(info.gameExtInfo.strContent);
						}
					}
					MemoryStream memoryStream = new MemoryStream();
					xDocument.Save(memoryStream);
					string text = Util.ByteArrayToString(memoryStream.GetBuffer());
					int num = text.IndexOf('<', 3);
					string text2 = text.Substring(num);
					result = text2;
				}
				else
				{
					result = null;
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("EmojiImageService", ex.ToString());
				result = null;
			}
			return result;
		}
		public static string getGameExtInfoXml(GameExtInfo info)
		{
			if (info != null && !string.IsNullOrEmpty(info.strContent))
			{
				string result;
				try
				{
					XDocument xDocument = XDocument.Parse("<gameext type=\"0\" content=\"0\" ></gameext>");
					XElement xElement = xDocument.Element("gameext");
					if (xElement != null)
					{
						xElement.Attribute("type").set_Value(string.Concat(info.nGameType));
						xElement.Attribute("content").set_Value(info.strContent);
					}
					MemoryStream memoryStream = new MemoryStream();
					xDocument.Save(memoryStream);
					string text = Util.ByteArrayToString(memoryStream.GetBuffer());
					int num = text.IndexOf('<', 3);
					string text2 = text.Substring(num);
					result = text2;
				}
				catch (Exception ex)
				{
					Log.smethod_1("EmojiImageService", ex.ToString());
					result = null;
				}
				return result;
			}
			return null;
		}
	}
}
